<?php

namespace App\Policies;

use Illuminate\Auth\Access\HandlesAuthorization;
use App\Models\Admin;
use App\Menu;

class MenuPolicy
{
    use HandlesAuthorization;

    /**
     * Create a new policy instance.
     *
     **/
    public function __construct()
    {
        //
    }

    public function before($user, $ability)
    {
        //TODO:
//        if ($user->isSuperAdmin()) {
//            return true;
//        }
    }

    /**
     * @param Admin $_admin 依赖注入当前用户
     * @param Menu $_menu 依赖注入要检查的菜单
     * @return bool
     */
    public function show(Admin $_admin, Menu $_menu)
    {
        return empty($_menu->roles)
            OR
                in_array($_admin->role, $_menu->roles) && !isset($_admin->groupRole)
            OR
                isset($_admin->groupRole) && in_array($_admin->groupRole, $_menu->roles);
    }
}
